package com.flash.controller.admin;

import com.flash.dto.SetmealDTO;
import com.flash.dto.SetmealPageQueryDTO;
import com.flash.result.PageResult;
import com.flash.result.Result;
import com.flash.service.SetMealService;
import com.flash.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "套餐相关接口")
@Slf4j
@RequestMapping("/admin/setmeal")
public class SetMealController {

    @Autowired
    private SetMealService setMealService;

    /**
     * 套餐分页查询
     * @param pageQueryDTO
     * @return
     */
    @ApiOperation("套餐分页查询")
    @GetMapping("/page")
    public Result<PageResult>page(SetmealPageQueryDTO pageQueryDTO){
        PageResult pageResult = setMealService.page(pageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("新增套餐")
    @PostMapping
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")
    public Result insertSetMeal(@RequestBody SetmealDTO setmealDTO){

        setMealService.insertSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @ApiOperation("根据id查询套餐")
    @GetMapping("/{id}")
    public Result<SetmealVO>getSetMealById(@PathVariable Long id){

        SetmealVO setmealVO =  setMealService.getSetMealById(id);
        return Result.success(setmealVO);
    }

    /**
     * 更新套餐
     * @param setmealDTO
     * @return
     */
    @ApiOperation("更新套餐")
    @PutMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result updateSetMeal(@RequestBody SetmealDTO setmealDTO){
        /*进行参数更新*/
        setMealService.updateSetMeal(setmealDTO);
        return Result.success();
    }

    /**
     * 批量套餐删除
     * @param ids
     * @return
     */
    @ApiOperation("批量套餐删除")
    @DeleteMapping
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetMeal(@RequestParam List<Long> ids){
        log.info("套餐删除");
        setMealService.deleteSetMeal(ids);
        return Result.success();
    }

    /**
     * 套餐的起售停售
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("套餐起售停售")
    @PostMapping("/status/{status}")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result changeStatus(@PathVariable int status,Long id){
        log.info("改变套餐状态");
        setMealService.changeStatus(status,id);
        return Result.success();
    }
}
